package com.wh.wisdomsite.xbox.utils;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/***
 * 获取IP工具类
 * @Package com.wh.wisdomsite.xbox.entity.client
 * @author 谢泽鹏
 * @date 2016年11月07日 下午15:10
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 * @version V1.0
 */
public class IpUtils {


    private static final Log logger = LogFactory.getLog(IpUtils.class);

    /**
     * 获取真实IP地址
     * 1. 服务器采用负载均衡时, 则先判断X_REAL_IP头文件, 请求IP会变成负载均衡服务器的IP。
     * 2. 根据X_FORWARDED_FOR取头文件IP地址。
     *    采集顺序规则(PROXY_CLIENT_IP, WL_PROXY_CLIENT_IP, HTTP_CLIENT_IP, HTTP_X_FORWARDED_IP)
     *    真实IP：取出IP为 192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100， 则真实IP为: 192.168.1.110
     * 3. 采用request.getRemoteAddr()远程IP地址。
     *    如为远程IP地址为(127.0.0.1 || 0:0:0:0:0:0:0:1), 则以本机网卡IP地址为准。
     * @param request
     * @return
     */
    private String getRemoteAddr(HttpServletRequest request) {
        //默认Ip，为设备到远程服务器IP
        String defaultIp = "0:0:0:0";
        //1.首先判断载均衡服务器的IP
        String remoteIp = request.getHeader("X-Real-IP");
        if (org.springframework.util.StringUtils.hasText(remoteIp)) {
            return remoteIp;
        }
        //2.根据X_FORWARDED_FOR取各种IP地址
        remoteIp = request.getHeader("X-Forwarded-For");
        if(remoteIp == null || remoteIp.length() == 0 || "unknown".equalsIgnoreCase(remoteIp)) {
            remoteIp = request.getHeader("Proxy-Client-IP");
        }
        if(remoteIp == null || remoteIp.length() == 0 || "unknown".equalsIgnoreCase(remoteIp)) {
            remoteIp = request.getHeader("WL-Proxy-Client-IP");
        }
        if (remoteIp == null || remoteIp.length() == 0 || "unknown".equalsIgnoreCase(remoteIp)) {
            remoteIp = request.getHeader("HTTP_CLIENT_IP");
        }
        if (remoteIp == null || remoteIp.length() == 0 || "unknown".equalsIgnoreCase(remoteIp)) {
            remoteIp = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (org.springframework.util.StringUtils.hasText(remoteIp)) {
            String[] ips = remoteIp.split(",");
            for (String firstIp : ips) {
                if (!"null".equalsIgnoreCase(firstIp)) {
                    return firstIp;
                }
            }
        } else {
            //3. 采用request.getRemoteAddr()远程IP地址。
            remoteIp = request.getRemoteAddr();
            if(org.springframework.util.StringUtils.hasText(remoteIp)){
                //根据网卡取本机配置的IP
                if(remoteIp.equals("127.0.0.1") || remoteIp.equals("0:0:0:0:0:0:0:1")){
                    try {
                        InetAddress inetAddress = InetAddress.getLocalHost();
                        remoteIp = inetAddress.getHostAddress();
                        if(!org.springframework.util.StringUtils.hasText(remoteIp)){
                            return defaultIp;
                        }
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                        logger.error("getRemoteAddr-->>getLocalHost()-->>UnknownHostException " + e.getMessage());
                    }
                }
                return remoteIp;
            }
        }
        return defaultIp;
    }


    public static boolean isIE(HttpServletRequest request){
        String agent = request.getHeader("User-Agent").toLowerCase();
        if(agent.indexOf("msie 7")>0){
            return true;
        }else if(agent.indexOf("msie 8")>0){
            return true;
        }else if(agent.indexOf("msie 9")>0){
            return true;
        }else if(agent.indexOf("msie 10")>0){
            return true;
        }else if(agent.indexOf("msie")>0){
            return true;
        }else if(agent.indexOf("opera")>0){
            return false;
        }else if(agent.indexOf("opera")>0) {
            return false;
        }else if(agent.indexOf("firefox")>0){
            return false;
        }else if(agent.indexOf("webkit")>0){
            return false;
        }else if(agent.indexOf("gecko")>0 && agent.indexOf("rv:11")>0){
            return true;
        }else{
            return false;
        }
    }
}
